<template>
  <div>
    <TabsView />
    <h1>宠物信息管理</h1>
    <el-button
      type="primary"
      plain
      @click="(dialogFormVisible = true), (flag = 0)"
      class="xdj"
      >添加</el-button
    >
    <el-table
      :data="
        tableData
          ? tableData.slice(
              (currentPage - 1) * pagesize,
              currentPage * pagesize
            )
          : []
      "
      style="width: 100%"
      :default-sort="{ prop: 'date', order: 'descending' }"
    >
      <el-table-column label="宠物ID" prop="id" width="180px">
      </el-table-column>
      <el-table-column label="宠物美照" prop="petImg" width="200px">
        <template slot-scope="scope">
          <img :src="scope.row.petImg" style="width: 80px" />
        </template>
      </el-table-column>
      <el-table-column label="宠物美名" prop="petName" width="180px">
      </el-table-column>
      <el-table-column label="宠物住址" prop="houseNum" width="200px">
      </el-table-column>
      <el-table-column label="宠物类别" prop="category" width="200px">
      </el-table-column>
      <el-table-column label="联系物主" prop="ownPhone" width="200px">
        <template slot-scope="scope">
          <el-tag type="success">{{ scope.row.ownPhone }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="宠物年龄" prop="petAge" width="200px">
      </el-table-column>
      <el-table-column align="right" width="260px">
        <template slot="header" slot-scope="{}">
          <el-input
            v-model="search"
            size="mini"
            placeholder="输入关键字搜索"
            @change="handleClick()"
          />
        </template>

        <template slot-scope="scope">
          <el-button
            type="success"
            plain
            class="xd"
            size="small"
            @click="xiugai(scope.row, (flag = 1))"
            >修改</el-button
          >
          <el-button
            type="danger"
            plain
            class="xd"
            @click="Delete(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 点击添加和修改的弹框 -->
    <el-dialog title="宠物" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="宠物ID" :label-width="formLabelWidth">
          <el-input v-model="form.houseId" autocomplete="off" class="srkw">{{
            form.id
          }}</el-input>
        </el-form-item>
        <el-form-item label="房屋ID" :label-width="formLabelWidth">
          <el-select v-model="form.houseNum" placeholder="请选择活动区域">
            <el-option
              :label="v.houseNum"
              :value="v.id"
              v-for="v in PetHouse"
              :key="v.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="宠物图片" :label-width="formLabelWidth">
          <el-input v-model="form.petImg" autocomplete="off" class="srkw">{{
            form.Img
          }}</el-input>
        </el-form-item>
        <el-form-item label="宠物美名" :label-width="formLabelWidth">
          <el-input v-model="form.petName" autocomplete="off" class="srkw">{{
            form.petName
          }}</el-input>
        </el-form-item>
        <el-form-item label="宠物类别" :label-width="formLabelWidth">
          <el-input v-model="form.category" autocomplete="off" class="srkw">{{
            form.category
          }}</el-input>
        </el-form-item>
        <el-form-item label="宠物年龄" :label-width="formLabelWidth">
          <el-input
            v-model="form.petAge"
            autocomplete="off"
            class="srkw"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addfunpet()">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 分页器 -->
    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="currentPage"
        :page-sizes="[3, 6, 9, 12]"
        :page-size="pagesize"
        layout="total, sizes, prev, pager, next, jumper"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import {
  lookpet,
  addpet,
  findpet,
  deletepet,
  petimg,
  gaipet,
  getPetHouse,
} from "../../apis/wyn/link.js";
import TabsView from "../pm/TabsView.vue";
export default {
  components: { TabsView },
  mounted() {
    // 查看宠物的所有信息(调用)
    this.petall();
    getPetHouse().then((ok) => {
      console.log(ok);
      this.PetHouse = ok.data.data;
    });
    // 添加里面的图片
    petimg().then((ok) => {
      console.log(ok);
    });
  },

  data() {
    return {
      tableData: [],
      arr: "",
      // 默认显示第几页
      currentPage: 1,
      pagesize: 3,
      fileList: [],
      search: "",
      dialogTableVisible: false,
      dialogFormVisible: false,
      PetHouse: "",
      form: {
        id: "",
        petImg: "",
        petName: "",
        houseNum: "",
        houseId: "",
        category: "",
        ownPhone: "",
        petAge: "",
        delivery: false,
        type: [],
        resource: "",
        desc: "",
      },
      formLabelWidth: "120px",
    };
  },

  // 删除的弹框的函数
  methods: {
    // 每页显示的条数
    handleSizeChange(pagesize) {
      this.pagesize = pagesize;
      console.log(pagesize);
    },
    handleCurrentChange(currentPage) {
      // this.currentPage = currentPage;

      if (currentPage > this.currentPage) {
        // this.currentPage=currentPage
        console.log(currentPage);
      } else {
        console.log(currentPage);
      }
    },
    // 查找宠物的单条信息
    handleClick() {
      console.log(this.search);
      findpet(this.search).then((ok) => {
        console.log(ok, 13232);
        this.tableData = ok.data.data;
        // 清空输入框的我问题
        this.search = "";
      });
    },

    //删除事件
    Delete(i) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$message({
            type: "success",
            message: "删除成功!",
          });
          deletepet(i).then((res) => {
            console.log(res);
            this.petall();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },

    // 添加宠物信息
    addfunpet() {
      this.dialogFormVisible = false
      if (this.flag === 0) {
        addpet(
          this.form.category,
          this.form.houseId,
          this.form.petAge,
          this.form.petImg,
          this.form.petName
        ).then((ok) => {
          console.log(ok.data, 999);
          lookpet().then((ok) => {
            // console.log(ok);
            this.tableData = ok.data.data;
            // this.arr = ok.data.data.length;
            // console.log(this.arr);
          });
        });
      } else {
        // 修改的请求
        gaipet(
          this.form.category,
          this.form.createTime,
          this.form.houseId,
          this.form.id,
          this.form.petAge,
          this.form.petImg,
          this.form.petName
        ).then((ok) => {
          console.log(ok);
          this.petall()
          this.dialogFormVisible = false;
        });
      }
    },

    // 查看宠物的所有信息的封装、
    petall() {
      lookpet().then((ok) => {
        console.log(ok, 111);
        this.tableData = ok.data.data;
        // console.log(ok);
      });
    },

    //修改事件
    xiugai(row) {
      console.log(row);
      this.flag = 1;
      this.form.category = row.category,
        this.form.createTime = row.createTime,
        this.form.houseId = row.houseId,
        this.form.houseNum = row.houseNum,
        this.form.id = row.id,
        this.form.petAge = row.petAge,
        this.form.petImg = row.petImg,
        this.form.petName = row.petName,
        this.form.updateTime = row.updateTime;
      this.dialogFormVisible = true;
    },
  },
};
</script>
<style scoped>
h1 {
  font-size: 30px;
  color: rgb(139, 231, 183);
  margin: 10px 0 20px;
}
.el-dropdown-link {
  cursor: pointer;
  color: #409eff;
}
.el-icon-arrow-down {
  font-size: 12px;
}
.xd {
  width: 60px;
  height: 35px;
  font-size: 10px;
  margin: 0 5px;
}
.xdj {
  width: 80px;
  height: 39px;
  margin: 0 0 8px;
}
.srkw {
  width: 400px;
  height: 20px;
}
</style>